package com.yx.system.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yx.common.annotation.DataFilter;
import com.yx.common.utils.R;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.dao.SysUserDao;
import com.yx.system.entity.SysUser;
import com.yx.system.service.SysUserRoleService;
import com.yx.system.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/** 系统用户 服务实现类
 * Created by wuye on 2017/11/27.
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private TbUserService tbUserService;

    @Override
    //@DataFilter(tableAlias = "u", user = false)
    public Page<SysUser> queryPageList(Page<SysUser> page, Map<String, Object> map) {
        page.setRecords(sysUserDao.queryPageList(page, map));
        return page;
    }

    @Override
    @DataFilter(tableAlias = "u", user = false)
    public List<SysUser> queryList(Map<String, Object> map){
        return sysUserDao.queryList(map);
    }

    @Override
    public List<String> queryAllPerms(Long userId) {
        return sysUserDao.queryAllPerms(userId);
    }

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return sysUserDao.queryAllMenuId(userId);
    }

    @Override
    public SysUser queryByUserName(String username) {
        return sysUserDao.queryByUserName(username);
    }

    @Override
    @Transactional
    public void save(SysUser user) {
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
        user.setSalt(salt);
        sysUserDao.insert(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    @Transactional
    public R beAuthor(TbUser tbUser, SysUser sysUser) {
        sysUser.setUsername(tbUser.getMobile());
        sysUser.setPassword(tbUser.getPassword());
        sysUser.setIdentity(1);
        sysUser.setStatus(1);
        sysUser.setMobile(tbUser.getMobile());
        sysUser.setEmail(tbUser.getEmail());
        sysUserDao.insert(sysUser);
        sysUserRoleService.authorRole(sysUser.getUserId(), 4L);
        //更新用户为作者
        tbUser.setIdentity(1);
        tbUser.setAuthorGrade(1);
        tbUserService.update(tbUser);
        return R.ok();
    }

    @Override
    public void updatePssword(SysUser sysUser) {
        sysUserDao.updateSysUser(sysUser);
    }

    @Override
    @Transactional
    public void update(SysUser user) {
        if(StringUtils.isBlank(user.getPassword())){
            user.setPassword(null);
        }else{
            user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
        }
        sysUserDao.updateById(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }



    @Override
    @Transactional
    public void deleteBatch(Long[] userId) {
        sysUserDao.deleteBatch(userId);
    }

}

